/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.sf.odinms.net.channel.handler;

import net.sf.odinms.client.IItem;
import net.sf.odinms.client.MapleClient;
import net.sf.odinms.client.MapleInventoryType;
import net.sf.odinms.net.AbstractMaplePacketHandler;
import net.sf.odinms.server.MapleInventoryManipulator;
import net.sf.odinms.tools.MaplePacketCreator;
import net.sf.odinms.tools.data.input.SeekableLittleEndianAccessor;

/**
 *
 * @author Bas
 */
public class OpenTreasureHandler extends AbstractMaplePacketHandler {
    private static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(OpenTreasureHandler.class);
    @Override
	public void handlePacket(SeekableLittleEndianAccessor slea, MapleClient c) {
        if (!c.getPlayer().isAlive()) {
			c.getSession().write(MaplePacketCreator.enableActions());
			return;
		}
        short slot = slea.readShort();
		int itemId = slea.readInt();
        IItem lol = c.getPlayer().getInventory(MapleInventoryType.ETC).getItem((byte)slot);
        if (lol != null && lol.getQuantity() > 0) {
            if (lol.getItemId() != itemId) {
				log.info("[h4x] Player {} is using an item not in the slot: {}", c.getPlayer().getName(), Integer.valueOf(itemId));
			}
            if (c.getPlayer().haveItem(5490000, 1, false, true)) {
                int ammount = itemId == 4280000 ? 3000 : 1000;
                c.getPlayer().modifyCSPoints(0, ammount);
                MapleInventoryManipulator.removeFromSlot(c, MapleInventoryType.ETC, (byte)slot, (short) 1, false);
                byte lulz = c.getPlayer().getInventory(MapleInventoryType.CASH).findById(5490000).getPosition();
                MapleInventoryManipulator.removeFromSlot(c, MapleInventoryType.CASH, lulz, (short) 1, false);
                c.getSession().write(MaplePacketCreator.serverNotice(5, "You have succesfully opened the treasure box and you have gained " + ammount + " nx"));
                c.getSession().write(MaplePacketCreator.openTreasure(itemId));
				c.getSession().write(MaplePacketCreator.enableActions());
				return;
            }
        } else {
			log.info("[h4x] Player {} is using an item he does not have: {}", c.getPlayer().getName(), Integer.valueOf(itemId));
		}
	}
}
